package vip.mtdp.asset.core.service;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.mtdp.asset.api.AccountService;
import vip.mtdp.asset.api.dto.AccountDTO;
import vip.mtdp.asset.api.request.OpenAccountRequest;
import vip.mtdp.asset.api.request.QueryAccountBalanceRequest;
import vip.mtdp.asset.core.dto.QueryBalancePageDTO;
import vip.mtdp.asset.core.manager.AccountManager;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description
 * @Author Evans.wang
 * @since 2023/10/31 16:33
 **/
@Slf4j
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountManager accountManager;

    /**
     * 开户
     *
     * @param request
     */
    @Override
    @Transactional
    public void open(OpenAccountRequest request) {
        accountManager.create(request);
    }

    /**
     * 账户信息查询(包括余额)
     *
     * @param vaultId
     * @param no
     * @return
     */
    @Override
    public AccountDTO queryByNo(Long vaultId, String chain, String no) {
        return accountManager.queryAccountByNo(vaultId, chain, no);
    }

    /**
     * 账户余额查询
     *
     * @param request
     * @return
     */
    @Override
    public List<AccountDTO.BalanceDTO> queryBalance(QueryAccountBalanceRequest request) {
        Long vaultId = request.getVaultId();
        String chain = request.getChain();
        String no = request.getNo();
        return accountManager.queryBalanceByNo(vaultId, chain, no);
    }

    /**
     * 分页查询账户余额
     *
     * @param request
     * @return
     */
    @Override
    public List<AccountDTO.BalanceDTO> queryBalanceByPage(QueryAccountBalanceRequest request) {
        QueryBalancePageDTO queryBalancePageDTO = new QueryBalancePageDTO();
        BeanUtil.copyProperties(request, queryBalancePageDTO);
        return accountManager.queryBalanceByPage(queryBalancePageDTO);
    }

    /**
     * 冻结
     *
     * @param accountId
     * @param coin
     * @param amount
     * @return
     */
    @Override
    @Transactional
    public boolean frozenBalance(Long accountId, String coin, BigDecimal amount) {
        return accountManager.frozenBalance(accountId, coin, amount);
    }

    /**
     * 解冻
     *
     * @param accountId
     * @param coin
     * @param amount
     * @return
     */
    @Override
    @Transactional
    public boolean unFrozenBalance(Long accountId, String coin, BigDecimal amount) {
        return accountManager.unFrozenBalance(accountId, coin, amount);
    }
}
